package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.utils.Constants;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/12/20  11:41
 */
@Controller
@RequestMapping("/main/storageRecord")
public class StorageRecordController extends BaseController {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(StorageRecordController.class);
    @Autowired
    private StorageRecordService storageRecordService;
    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/page")
    public String page(Model model, StorageRecord condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<StorageRecord> page=null;
        try {
            //查询用户分页信息
            page=storageRecordService.findPage(condition, pageNo, pageSize);
            //查询所有供货商
            List<Supplier> supplierList=supplierService.getAll();
            //将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("supplierList",supplierList);
            model.addAttribute("pageInfo",page);
            model.addAttribute("condition",condition);
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/page";
    }

    //跳转到添加页面
    @RequestMapping("/save")
    public String tosave(Model model){
        try {
            List<Supplier> supplierList=supplierService.getAll();
            model.addAttribute("supplierList",supplierList);
            model.addAttribute("storageRecord",new StorageRecord());
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 tosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/save";
    }

    //做添加、修改操作
    @PostMapping("/save")
    public String dosave(StorageRecord storageRecord){
        try {
            SysUser sysUser=session(Constants.SESSION_USER);
            boolean ok=false;
            if (storageRecord.getId()==null){
                storageRecord.setCreatedUserId(sysUser.getId());
                storageRecord.setCreatedTime(new Date());
                ok=storageRecordService.insert(storageRecord);
            }else {
                storageRecord.setUpdatedUserId(sysUser.getId());
                storageRecord.setUpdatedTime(new Date());
                ok=storageRecordService.update(storageRecord);
            }
            if (ok){
                return redirect("/main/storageRecord/page");
            }
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 dosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/save";
    }

    //点击修改按钮数据显示
    @GetMapping("/edit")
    public String edit(Model model,@RequestParam("id") Long id){
        StorageRecord storageRecord=null;
        List<Supplier> supplierList=null;
        try {
            storageRecord=storageRecordService.selectById(id);
            supplierList=supplierService.getAll();
            model.addAttribute("supplierList",supplierList);
            model.addAttribute("storageRecord",storageRecord);
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 dosave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/save";
    }

    //详情
    @GetMapping("/view/{id}")
    public Object view(@PathVariable("id") Long id,Model model){
        StorageRecord storageRecord=null;
        try {
            storageRecord=storageRecordService.selectById(id);
            model.addAttribute("storageRecord",storageRecord);
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "storageRecord/view";
    }

    //查重
    @GetMapping("/srCodeExist")
    @ResponseBody
    public Object srCodeExis(@RequestParam("srCode") String srCode){
        StorageRecord storageRecord=null;
        Map<String,Object> map=new HashMap<>();
        try {
            storageRecord=storageRecordService.selectCode(srCode);
            if (storageRecord!=null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 srCodeExis 方法执行发生错误。 ", e);
            map.put("exist",-1);
            map.put("error","服务器繁忙，请稍后再试");
        }
        return map;
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok=storageRecordService.delete(id);
            if (ok){
                map.put("code",200);
            }else {
                map.put("code",500);
                map.put("msg","数据删除失败");
            }
        }catch (Exception e) {
            LOGGER.error("StorageRecordController 中的 delete 方法执行发生错误。 ", e);
            map.put("code",500);
            map.put("msg","数据删除失败");
        }
        return map;
    }
}
